package org.sean.framework;

import org.sean.framework.bean.BaseResult;
import org.sean.framework.code.StatusInfo;
import org.sean.framework.exception.StatusException;
import org.sean.framework.feign.DemoClient;
import org.sean.framework.feign.User;
import org.sean.framework.util.ThreadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
@EnableFeignClients("org.sean.framework.feign")
public class Application {

    static ConfigurableApplicationContext applicationContext;
    @Autowired
    DemoClient demoClient;

    public static void main(String[] args) {
        applicationContext = SpringApplication.run(Application.class, args);
    }

    @GetMapping("normal")
    public User normal() {
        return User.builder().name("test name").phone("1111").build();
    }

    @GetMapping("excetpion")
    public User excetpion() {
        throw new StatusException(StatusInfo.notLoginError());
    }

    @GetMapping("callNormal")
    public User callNormal() {
        return demoClient.normal().getData();
    }

    @GetMapping("callException")
    public User callException() {
        return demoClient.excetpion().getData();
    }

    @GetMapping("noBaseResult")
    public User noBasesult() {
        return null;
    }

    @GetMapping("sleep")
    public User sleep(@RequestParam("s") int s) {
        ThreadUtil.sleep(s * 1000);
        return User.builder().name("sleep").phone("phone test").build();
    }

    @GetMapping("callSleep")
    public User callSleep(@RequestParam("s") int s) {
        BaseResult<User> result = demoClient.sleep(s);
        return result.getData();
    }

}
